﻿//----------------------------------------------------------------------//
//                                                                      //
//                          钱文田  2007-05-17                          //
//                                                                      //
//              对用户信息提示、错误提示和正在加载进行显示              //
//                                                                      //
//                  需要外面调用页面引用 Common.js文件                  //
//                                                                      //
//----------------------------------------------------------------------//

/// <summary>
/// 自定义提示框类
/// </summary>
function MessageBox()
{
    throw new Error("自定义提示框类(MessageBox)是静态类, 不能创建对象!");
}

/// <summary>
/// 显示提示消息框
/// </summary>
/// <param name="type">提示框类型 (info: 提醒; error: 错误; load: 正在加载 )</param>
/// <param name="html">提示框显示的HTML内容</param>
/// <param name="detail">提示框需要显示的详细内容</param>
/// <param name="notify">提示框关闭后需要执行的函数</param>
function MessageBox.show(type, html, detail, notify)
{
    MessageBox.notify = notify;

    var messageType = type.toLowerCase();
    
    var iframeBox = top.document.getElementById("MessageBoxIFrame");
    if (!iframeBox)
    {
        var iframeBox = top.document.createElement("IFRAME");
        top.document.body.insertBefore(iframeBox, top.document.body.firstChild);
        iframeBox.outerHTML = '<iframe id="MessageBoxIFrame" src="' + ApplicationPath + '/App_Themes' + ThemePath + '/messagebox.htm" style="display: none; z-index: 9; position: absolute;" frameBorder="0" scrolling="no" width="398" height="188"></iframe>';
        iframeBox = top.document.getElementById("MessageBoxIFrame");
    }

    var iframeFrame = top.document.getElementById("MessageFrameIFrame");
    if (!iframeFrame)
    {
        var iframeFrame = top.document.createElement("IFRAME");
        top.document.body.insertBefore(iframeFrame, top.document.body.firstChild);
        iframeFrame.outerHTML = '<iframe id="MessageFrameIFrame" src="' + ApplicationPath + '/App_Themes' + ThemePath + '/messageframe.htm" style="display: none; z-index: 9; position: absolute; filter:Alpha(opacity=50)" frameBorder="0" scrolling="no" width="100%" height="100%"></iframe>';
        iframeFrame = top.document.getElementById("MessageFrameIFrame");
    }

    //
    // 把当前调用的窗体保存在最顶层窗体的变量中, 在框架页面中关闭提示框用
    //
    top.window.messgeBoxWindow = window;

    setTimeout(show_interval, 1);
    
    /// <summary>
    /// 显示提示消息框计时器调用
    /// </summary>
    function show_interval()
    {
        // 若提示框网页没有加载完成, 50毫秒后继续调用本函数
        if (iframeBox.readyState == "loading" || iframeFrame.readyState == "loading")
        {
            setTimeout(show_interval, 50);
            return;
        }

        // 若提示框当前是显示状态, 先隐藏
        if (iframeBox.style.display != "none" || iframeFrame.style.display != "none")
        {
            MessageBox.hide();
        }

        // 若提示框还有HTML元素没有加载完毕, 50毫秒后继续调用本函数
        if (!top.MessageFrameIFrame.document || !top.MessageBoxIFrame.document || !top.MessageBoxIFrame.document.getElementById("MsgType") ||
            !top.MessageBoxIFrame.document.getElementById("MsgTxt") || !top.MessageBoxIFrame.document.getElementById("MsgDetail"))
        {
            setTimeout(show_interval, 50);
            return;
        }

        // 根据提示类型设置图片
        top.MessageBoxIFrame.document.getElementById("MsgType").src = "images/" + messageType + ".gif";

        // 设置提示信息
        top.MessageBoxIFrame.document.getElementById("MsgTxt").innerHTML = html;

        // 设置详细信息
        var msgDetail = top.MessageBoxIFrame.document.getElementById("MsgDetail");
        if (detail == null || detail == "")
        {
            msgDetail.style.display = "none";
        }
        else
        {
            msgDetail.style.display = "";
            msgDetail.href = "javascript:showPopup('" + detail.toJSString() + "')";
        }

        calcposition();
        MessageBox.windowResizePre = window.onresize;
        window.onresize = windowresize;
        MessageBox.windowScrollPre = window.onscroll;
        window.onscroll = windowscroll;

        // 若是加载等待, 设置确定按钮不显示
        if (messageType == "load")
        {
            top.MessageBoxIFrame.document.getElementById("Footer").style.display = "none";
            if (MessageBox.notify)
            {
                try
                {
                    MessageBox.notify();
                }
                finally
                {
                    MessageBox.notify = null;
                }
            }
        }
        else
        {
            top.MessageBoxIFrame.document.getElementById("Footer").style.display = "";
        }

        iframeBox.style.display = "";
        iframeFrame.style.display = "";
        top.MessageBoxIFrame.focus();
        MessageBox.initFinish = true;
    }

    /// <summary>
    /// 根据窗口客户端区域、滚动条位置、消息框宽高来计算显示位置
    /// </summary>
    function calcposition()
    {
        iframeFrame.style.top = top.document.body.scrollTop;
        iframeFrame.style.left = top.document.body.scrollLeft;

        iframeBox.style.top = top.document.body.scrollTop + (top.document.body.clientHeight - iframeBox.height) / 2;
        iframeBox.style.left = top.document.body.scrollLeft + (top.document.body.clientWidth - iframeBox.width) / 2;
    }

    /// <summary>
    /// 页面onresize事件中加入调整提示框位置钩子函数
    /// </summary>
    function windowresize()
    {
        calcposition();
        if (MessageBox.windowResizePre)
        {
            MessageBox.windowResizePre();
        }
    }

    /// <summary>
    /// 在页面onscroll事件中加入调整提示框位置钩子函数
    /// </summary>
    function windowscroll()
    {
        calcposition();
        if (MessageBox.windowScrollPre)
        {
            MessageBox.windowScrollPre();
        }
    }
}

/// <summary>
/// 隐藏提示消息框
/// </summary>
function MessageBox.hide()
{
    if (!MessageBox.initFinish)
    {
        setTimeout(MessageBox.hide, 50);
        return;
    }

    var iframeBox = top.document.getElementById("MessageBoxIFrame");
    var iframeFrame = top.document.getElementById("MessageFrameIFrame");

    iframeBox.style.display = "none";
    iframeFrame.style.display = "none";
    window.onresize = MessageBox.windowResizePre;
    window.onscroll = MessageBox.windowScrollPre;

    if (top.MessageBoxIFrame.document.getElementById("Footer").style.display = "none")
    {
        if (MessageBox.notify)
        {
            try
            {
                MessageBox.notify();
            }
            finally
            {
                MessageBox.notify = null;
            }
        }
    }
}

/// <summary>
/// 提示框关闭后需要执行的函数
/// </summary>
MessageBox.notify = null;

/// <summary>
/// 提示框是否已经加载完成
/// </summary>
MessageBox.initFinish = false;

/// <summary>
/// 保存页面尺寸变化的事件
/// </summary>
MessageBox.windowResizePre = null;

/// <summary>
/// 保存页面滚动条的事件
/// </summary>
MessageBox.windowScrollPre = null;